package com.jxtc.enterprise.tenant.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jxtc.enterprise.common.entity.DeliveryRider;
import com.jxtc.enterprise.common.entity.Store;
import com.jxtc.enterprise.common.exception.ModifyFailureException;
import com.jxtc.enterprise.common.mapper.RiderMapper;
import com.jxtc.enterprise.common.mapper.StoreMapper;
import com.jxtc.enterprise.tenant.vo.req.MerchantPasswordUpdateReqVo;
import com.jxtc.enterprise.tenant.vo.req.RiderUpdateReqVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ExternalCallService {

    private final StoreMapper storeMapper;
    private final RiderMapper riderMapper;

    @Transactional(rollbackFor = ModifyFailureException.class)
    public void editMerchantPassword(MerchantPasswordUpdateReqVo reqVo) {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<Store>()
                .eq(Store::getMerchantPhone, reqVo.getPhone())
                .eq(Store::getDelFlag, false)
                // 仅查询店铺 ID 即可，减少数据传输
                .select(Store::getId);
        List<Store> storeList = storeMapper.selectList(queryWrapper);
        if (storeList == null || storeList.isEmpty()) {
            log.warn("/editPassword, 手机号不存在: {}", reqVo.getPhone());
            throw new IllegalArgumentException("手机号不存在");
        }

        Store store = new Store();
        // TODO: 当前密码为明文形式存储
        store.setMerchantLoginPasswd(reqVo.getPassword());
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<Store>()
                .eq(Store::getMerchantPhone, reqVo.getPhone())
                .eq(Store::getDelFlag, false);
        int updateCount = storeMapper.update(store, updateWrapper);
        // 如果执行更新影响的行数不等于查询出来的数据行数，则认为更新失败，触发回滚
        if (updateCount != storeList.size()) {
            log.error("/editPassword, 更新密码失败, phone: {}, password: {}", reqVo.getPhone(), reqVo.getPassword());
            throw new ModifyFailureException("更新密码失败");
        }
    }


    @Transactional(rollbackFor = ModifyFailureException.class)
    public void editRider(RiderUpdateReqVo reqVo) {
        LambdaQueryWrapper<DeliveryRider> queryWrapper = new LambdaQueryWrapper<DeliveryRider>()
                .eq(DeliveryRider::getPhone, reqVo.getPhone())
                .eq(DeliveryRider::getDelFlag, false)
                // 仅查询配送员 ID 即可，减少数据传输
                .select(DeliveryRider::getId);
        List<DeliveryRider> riderList = riderMapper.selectList(queryWrapper);
        if (riderList == null || riderList.isEmpty()) {
            log.warn("/editRider, 手机号不存在: {}", reqVo.getPhone());
            throw new IllegalArgumentException("手机号不存在");
        }

        DeliveryRider rider = new DeliveryRider();
        if (StringUtils.hasText(reqVo.getName())) {
            rider.setName(reqVo.getName());
        }
        if (StringUtils.hasText(reqVo.getPassword())) {
            // TODO: 当前密码为明文形式存储
            rider.setPassword(reqVo.getPassword());
        }
        LambdaUpdateWrapper<DeliveryRider> updateWrapper = new LambdaUpdateWrapper<DeliveryRider>()
                .eq(DeliveryRider::getPhone, reqVo.getPhone())
                .eq(DeliveryRider::getDelFlag, false);
        int updateCount = riderMapper.update(rider, updateWrapper);
        // 如果执行更新影响的行数不等于查询出来的数据行数，则认为更新失败，触发回滚
        if (updateCount != riderList.size()) {
            log.error("/editRider, 更新配送员信息失败, phone: {}, DeliveryRider: {}", reqVo.getPhone(), JSONObject.toJSONString(rider));
            throw new ModifyFailureException("更新配送员信息失败");
        }
    }
}
